public class Work01 {
    public int countCharacters(String[] words, String chars) {
        int[] charCount = new int[26];
        for (int i = 0; i < chars.length(); i++) {
            charCount[chars.charAt(i) - 'a']++;
        }
        int res = 0;
        for (String word : words) {
            int length = word.length();
            int[] charNum = new int[26];
            boolean isMaster = true;
            for (int i = 0; i < length; i++) {
                char c = word.charAt(i);
                int num = ++charNum[c - 'a'];
                if (charCount[c - 'a'] < num) {
                    isMaster = false;
                    break;
                }
            }
            if (isMaster) res += length;
        }
        return res;
    }
}
